import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.location.LocationException;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import net.sf.microlog.core.PropertyConfigurator;


public class PlayByLocation extends MIDlet implements CommandListener {

	private TextBox messages;
	private Form form;
	private Display display;
	private PlayerManager pm;
	private Locator locator;
	
	private Command stopCommand;
	private Command pauseCommand;
	private Command startCommand;
	private Command exitCommand;
	
	public PlayByLocation() {
//		PropertyConfigurator.configure("/microlog.properties"); //loads the configuration file
		
		pm = PlayerManager.getPlayerManager();
		
		display = Display.getDisplay(this);
		
		form = new Form("PlayByLocation");
		
		stopCommand = new Command("Stop", Command.STOP, 1);
	    pauseCommand = new Command("Pause", Command.ITEM, 1);
	    startCommand = new Command("Start", Command.ITEM, 1);
	    exitCommand = new Command("Exit", Command.EXIT, 0);
	    
	    form.addCommand(exitCommand);
	    form.addCommand(startCommand);
//	    form.addCommand(pauseCommand);
//	    form.addCommand(stopCommand);
	    
	    form.setCommandListener(this);
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		pm.closePlayer();
	}

	protected void pauseApp() {
		// TODO Auto-generated method stub
	}

	protected void startApp() throws MIDletStateChangeException {
		try {
			locator = new Locator();
		} catch (LocationException e) {
			e.printStackTrace();
		}
		display.setCurrent(form);
	}

	public void commandAction(Command cmd, Displayable disp) {
		if(disp instanceof Form) {
			if(cmd == startCommand) {
				pm.startPlayer();
				
				form.removeCommand(startCommand);
				form.addCommand(pauseCommand);
				form.addCommand(stopCommand);
			}
			else if(cmd == pauseCommand) {
				pm.stopPlayer();
				
				form.removeCommand(pauseCommand);
				form.addCommand(startCommand);
			}
			else if(cmd == stopCommand) {
				pm.closePlayer();
				
				form.removeCommand(stopCommand);
				form.removeCommand(pauseCommand);
				form.addCommand(startCommand);
			}
			else if(cmd == exitCommand) {
				try {
					destroyApp(false);
				} catch (MIDletStateChangeException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
